package com.ygqh.baby.service.impl;

import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.mapper.YgReturnOrderDetailMapper;
import com.ygqh.baby.model.YgExchangeOrderDetail;
import com.ygqh.baby.po.YgReturnOrderDetail;
import com.ygqh.baby.po.YgReturnOrderDetailExample;
import com.ygqh.baby.service.YgReturnOrderDetailService;
import com.ygqh.baby.template.AbstractSplitListTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.*;

@Service
public class YgReturnOrderDetailServiceImpl implements YgReturnOrderDetailService {

	@Autowired
	private YgReturnOrderDetailMapper ygReturnOrderDetailMapper;

	@Override
	public List<YgReturnOrderDetail> find() {
		YgReturnOrderDetailExample example = new YgReturnOrderDetailExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygReturnOrderDetailMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgReturnOrderDetailExample example = new YgReturnOrderDetailExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygReturnOrderDetailMapper.countByExample(example);
	}

	@Override
	public YgReturnOrderDetail findById(Long id) {
		return ygReturnOrderDetailMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(YgReturnOrderDetail ygReturnOrderDetail) {
		return ygReturnOrderDetailMapper.insertSelective(ygReturnOrderDetail);
	}

	@Override
	public int update(YgReturnOrderDetail ygReturnOrderDetail) {
		return ygReturnOrderDetailMapper.updateByPrimaryKeySelective(ygReturnOrderDetail);
	}

	@Override
	public ResultSet<YgReturnOrderDetail> search(QueryInfo queryInfo, String q) {
		List<YgReturnOrderDetail> list = ygReturnOrderDetailMapper.selectSuper(queryInfo, q);
		int count = ygReturnOrderDetailMapper.countSuper(q);
		ResultSet<YgReturnOrderDetail> resultSet = new ResultSet<YgReturnOrderDetail>(count, list);
		return resultSet;
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		YgReturnOrderDetailExample example = new YgReturnOrderDetailExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		YgReturnOrderDetail record = new YgReturnOrderDetail();
		record.setStatus(DataStatus.Delete);

		return ygReturnOrderDetailMapper.updateByExampleSelective(record, example);
	}

	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[] { id });
	}

	@Override
	public int saveOrUpdate(YgReturnOrderDetail ygReturnOrderDetail) {
		int r = 0;
		if (ygReturnOrderDetail.getId() != null) {
			r = this.update(ygReturnOrderDetail);
		} else {
			r = this.save(ygReturnOrderDetail);
		}
		return r;
	}

	@Override
	public List<YgReturnOrderDetail> findByReturnCode(String returnCode) {
		YgReturnOrderDetailExample example = new YgReturnOrderDetailExample();
		example.createCriteria().andReturnCodeEqualTo(returnCode);
		List<YgReturnOrderDetail> list = ygReturnOrderDetailMapper.selectByExample(example);
		return list;
	}

	@Override
	public int saveExchangeOrderDetailBatch(List<YgExchangeOrderDetail> exchangeDetail) {
		return ygReturnOrderDetailMapper.saveExchangeOrderDetailBatch(exchangeDetail);
	}

	@Override
	public YgReturnOrderDetail findByReturnCodeWithSkuId(String returnCode, Long skuId) {
		YgReturnOrderDetailExample example = new YgReturnOrderDetailExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name()).andReturnCodeEqualTo(returnCode)
				.andSourceSkuIdEqualTo(skuId);
		List<YgReturnOrderDetail> list = ygReturnOrderDetailMapper.selectByExample(example);
		return CollectionUtils.isEmpty(list) ? null : list.get(0);
	}

	@Override
	public int updateSendBatch(List<YgReturnOrderDetail> detailList) {
		if (CollectionUtils.isEmpty(detailList)) {
			return 0;
		}
		return ygReturnOrderDetailMapper.updateSendBatch(detailList);
	}

	@Override
	public List<Map<String, Object>> getReturnInStockDetail(String returnType, Date startDate, Date endDate,
			String[] questionDesc) {

		return ygReturnOrderDetailMapper.selectReturnInStockDetail(returnType, startDate, endDate, questionDesc);
	}

	@Override
	public int updateDetailStatusBatch(List<YgReturnOrderDetail> rodList) {
		if (!CollectionUtils.isEmpty(rodList)) {
			ygReturnOrderDetailMapper.updateDetailStatusBatch(rodList);
		}
		return 0;
	}

	@Override
	public int saveBatch(List<YgReturnOrderDetail> returnOrderDetailList) {
		return new AbstractSplitListTemplate<YgReturnOrderDetail>(returnOrderDetailList) {
			@Override
			public int execute(List<YgReturnOrderDetail> recordList) {
				return ygReturnOrderDetailMapper.insertBatch(recordList);
			}
		}.split();
	}

	@Override
	public List<YgReturnOrderDetail> findByReturnCodeList(List<String> returnCodeList) {
		if (CollectionUtils.isEmpty(returnCodeList)) {
			return Collections.emptyList();
		}
		return ygReturnOrderDetailMapper.selectByReturnCodeList(returnCodeList);
	}

	@Override
	public int updatePriceBatch(List<YgReturnOrderDetail> detailList) {
		return new AbstractSplitListTemplate<YgReturnOrderDetail>(detailList) {
			@Override
			public int execute(List<YgReturnOrderDetail> recordList) {
				return ygReturnOrderDetailMapper.updatePriceBatch(recordList);
			}
		}.split();
	}

}
